//
//  DetailStateOfCertifyView.swift
//  cysLibrarySys
//
//  Created by 曹亚索 on 2021/7/6.
//

import UIKit

class DetailStateView: UIView {

    private let commonColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
    private let passColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
    private let certifyingColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
    private let failColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1)
    private let labelWidth: CGFloat = 200.0
    private let labelHeight: CGFloat = 50.0
    private let imageViewWidth: CGFloat = 50.0
    private let imageViewHeight: CGFloat = 50.0
    private let firstLabelTopMargin: CGFloat = 50
    private let labelSpacing: CGFloat = 40
    
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = currentBook!.face
        return imageView
    }()
    
    private lazy var bookNameLabel: UILabel = {
        let label = UILabel()
        label.textColor = commonColor
        label.textAlignment = .center
        label.adjustsFontSizeToFitWidth = true
        label.font = UIFont(name: "System", size: 10)
        label.text = "<<\(currentBook!.name ?? "")>>"
        return label
    }()
    
    private lazy var bookAuthorLabel: UILabel = {
        let label = UILabel()
        label.textColor = commonColor
        label.textAlignment = .center
        label.adjustsFontSizeToFitWidth = true
        label.font = UIFont(name: "System", size: 10)
        label.text = currentBook!.author
        return label
    }()
    
    private lazy var firstStateLabel: UILabel = {
        let label = UILabel()
        label.textColor = passColor
        label.textAlignment = .center
        label.font = UIFont(name: "System", size: 12)
        label.text = "提交审核申请"
        return label
    }()
    
    private lazy var secondStateLabel: UILabel = {
        let label = UILabel()
        label.textColor = passColor
        label.textAlignment = .center
        label.font = UIFont(name: "System", size: 12)
        label.text = "正在审核..."
        return label
    }()
    
    private lazy var thirdStateLabel: UILabel = {
        let label = UILabel()
        label.textColor = certifyingColor
        label.textAlignment = .center
        label.font = UIFont(name: "System", size: 12)
        label.text = "审核结果"
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpSelf()
        setUpFirstStateLabel()
        self.addSubview(firstStateLabel)
        setUpAuthorLabel()
        self.addSubview(bookAuthorLabel)
        setUpSecondStateLabel()
        self.addSubview(secondStateLabel)
        setUpThirdStateLabel()
        self.addSubview(thirdStateLabel)
        setUpBookNameLabel()
        self.addSubview(bookNameLabel)
        setUpImageView()
        self.addSubview(imageView)
        setUpLines()
    }
    
    private func setUpSelf(){
        self.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        self.layer.shadowColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
        self.layer.shadowOffset = CGSize(width: 10, height: 10)
        self.layer.shadowRadius = 10
        self.layer.shadowOpacity = 1
        self.layer.cornerRadius = self.frame.width/10
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissDetailStateView(tap:))))
    }
    
    @objc func dismissDetailStateView(tap:UIGestureRecognizer){
        switch tap.state {
        case .ended:
            print("inside view")
        default:
            break
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpImageView(){
        imageView.frame = CGRect(origin: .zero, size: CGSize(width: imageViewWidth, height: imageViewHeight))
        imageView.center.x = self.center.x
        imageView.center.y = bookNameLabel.center.y - labelSpacing * 1.5
    }
    
    private func setUpBookNameLabel(){
        bookNameLabel.frame = CGRect(origin: .zero, size: CGSize(width: labelWidth, height: labelHeight))
        bookNameLabel.center.x = self.center.x
        bookNameLabel.center.y = bookAuthorLabel.center.y - labelSpacing
    }
    
    private func setUpAuthorLabel(){
        bookAuthorLabel.frame = CGRect(origin: .zero, size: CGSize(width: labelWidth, height: labelHeight))
        bookAuthorLabel.center.x = self.center.x
        bookAuthorLabel.center.y = firstStateLabel.center.y - labelSpacing * 2
    }
    
    private func setUpFirstStateLabel(){
        firstStateLabel.frame = CGRect(origin: .zero, size: CGSize(width: labelWidth, height: labelHeight))
        firstStateLabel.center.x = self.center.x
        firstStateLabel.center.y = self.center.y + labelSpacing
    }
    
    private func setUpSecondStateLabel(){
        secondStateLabel.frame = CGRect(origin: .zero, size: CGSize(width: labelWidth, height: labelHeight))
        secondStateLabel.center.x = self.center.x
        secondStateLabel.center.y = firstStateLabel.center.y + labelSpacing
    }
    
    private func setUpThirdStateLabel(){
        thirdStateLabel.frame = CGRect(origin: .zero, size: CGSize(width: labelWidth, height: labelHeight))
        thirdStateLabel.center.x = self.center.x
        thirdStateLabel.center.y = secondStateLabel.center.y + labelSpacing
    }
    
    private func setUpLines(){
        addALineForView(fromPointX: 0, fromPointY: bookAuthorLabel.frame.maxY, toPointX: self.frame.maxX, toPointY: bookAuthorLabel.frame.maxY)
        addALineForView(fromPointX: self.center.x, fromPointY: secondStateLabel.frame.minY, toPointX: self.center.x, toPointY: firstStateLabel.frame.maxY)
        addALineForView(fromPointX: self.center.x, fromPointY: thirdStateLabel.frame.minY, toPointX: self.center.x, toPointY: secondStateLabel.frame.maxY)
    }
    
    private func addALineForView(fromPointX:CGFloat,fromPointY:CGFloat,toPointX:CGFloat,toPointY:CGFloat) {
        let linePath = UIBezierPath()
        linePath.move(to: CGPoint.init(x: fromPointX, y: fromPointY))
        linePath.addLine(to: CGPoint.init(x: toPointX, y: toPointY))
        let lineLayer = CAShapeLayer()
        lineLayer.lineWidth = 2
        lineLayer.strokeColor = UIColor.black.cgColor
        lineLayer.path = linePath.cgPath
        lineLayer.fillColor = UIColor.clear.cgColor
        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0
        animation.toValue = 1
        animation.duration = 0.5
        lineLayer.add(animation, forKey: "")
        self.layer.addSublayer(lineLayer)
    }
    

}
